home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Just Call Me Internet
/
Just Call Me Internet.iso
/
archives
/
com
/
internet
/
stik
/
gls002b5.zoo
/
debug.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-09-21
|
5KB
|
267 lines
#include <stdio.h>
#include <stdlib.h>
#include <support.h>
#include <string.h>
#include <osbind.h>
#include <mintbind.h>
#include <stdarg.h>
#include <unistd.h>
#define DAEMON /* to get "int caller_pid, " */
#include "global.h"
#include "pipe.h"
#ifdef DEBUG
#define SYSVAR_bootdev (*((short *)0x446UL))
extern char cfg_filename[160]; /* Used to build debug log filename */
static char DEBUG_LOG[160];
static char DEBUG_OLD[160];
static char *op_names[OP_MAX+1] = {
NULL,
"OP_TCP_OPEN",
"OP_TCP_CLOSE",
"OP_TCP_SEND",
"OP_TCP_WAIT_STATE",
"OP_UDP_OPEN",
"OP_UDP_CLOSE",
"OP_UDP_SEND",
"OP_CNBYTE_COUNT",
"OP_CNGET_CHAR",
"OP_CNGET_NDB",
"OP_CNGET_BLOCK",
"OP_RESOLVE",
"OP_CNGETINFO",
"OP_KRMALLOC",
"OP_KRFREE",
"OP_KRGETFREE",
"OP_KRREALLOC",
"OP_GETVSTR",
"OP_SETVSTR",
};
static char *prologs[] = {
"", /* DBG_NOMSG */
"): FATAL: ", /* DBG_FATAL */
"): ERROR: ", /* DBG_ERROR */
"): ", /* DBG_SYSCALL */
"): TRACE: ", /* DBG_TRACE */
};
typedef char *charp;
typedef void *voidp;
int debug_level = 0;
static int get_bootdrive(void)
{
return (int)SYSVAR_bootdev;
}
static char *get_procname(int pid)
{
static char buf[FILENAME_MAX + 5];
static char oops[] = "<<???>>";
long h = 0, i;
char *s;
h = Dopendir("U:\\PROC\\", 0);
if (h < 0) return oops;
while ((i = Dreaddir(sizeof(buf), h, buf)) == 0) {
s = strchr(buf + 4, '.');
if (!s) continue;
if (atoi(s+1) != pid) continue;
*s = '\0'; break; /* found our process */
}
Dclosedir(h);
return (i == 0 ? buf+4 : oops);
}
int init_debug()
{
register char *s = do_getvstr(Pgetpid(), GS_TRACE);
long l;
debug_level = atoi(s);
if (debug_level < DBG_NOMSG || debug_level > DBG_TRACE) {
Cconws("Debug level out of range; defaulting to zero\r\n");
debug_level = 0;
}
/* Put the trace log in the same directory as the config file. */
strcpy(DEBUG_LOG, cfg_filename);
s = strrchr(DEBUG_LOG, '\\');
if (!s) {
Cconws("No path specified for config file\r\n");
return 0;
}
s[1] = '\0';
strcpy(DEBUG_OLD, DEBUG_LOG);
strcat(DEBUG_LOG, "GLUESTIK.LOG");
strcat(DEBUG_OLD, "GLUESTIK.OLD");
Fdelete(DEBUG_OLD);
Frename(0, DEBUG_LOG, DEBUG_OLD);
l = Fcreate(DEBUG_LOG, 0);
if (l < 0) {
Cconws("Unable to create trace log ");
Cconws(DEBUG_LOG);
Cconws("\r\n");
return 0;
}
Fclose((int)(short)l);
if (Psem_create(DEBUG_SEM) < 0) {
Cconws("Unable to create trace log semaphore\r\n");
return 0;
}
Psem_release(DEBUG_SEM);
return 1;
}
void log_entry(int pid, int lev, const char *fmt, ...)
{
va_list ap;
char line[256];
register const char *s;
register char *t;
char *u;
void *v;
char buf[20];
long l;
unsigned long ul;
int fd;
if (lev > debug_level)
return;
strcpy(line, "pid ");
_itoa(pid, line+4, 10);
strcat(line, " (");
strcat(line, get_procname(pid));
strcat(line, prologs[lev]);
va_start(ap, fmt);
for (s = fmt, t = line+strlen(line); *s; s++) {
if (*s != '%') {
*t++ = *s;
continue;
}
switch (*++s) {
case '%':
*t++ = '%';
break;
case 's':
u = va_arg(ap, charp);
strcpy(t, u);
t += strlen(t);
break;
case 'S':
l = (long)va_arg(ap, int);
u = va_arg(ap, charp);
strncpy(t, u, l);
t += l;
break;
case 'i':
case 'd':
l = (long)va_arg(ap, int);
_ltoa(l, t, 10);
t += strlen(t);
break;
case 'x':
*t++ = '0'; *t++ = 'x';
l = (long)va_arg(ap, unsigned int);
_ltoa(l, t, 16);
t += strlen(t);
break;
case 'l':
l = va_arg(ap, long);
_ltoa(l, t, 10);
t += strlen(t);
break;
case 'X':
*t++ = '0'; *t++ = 'x';
ul = va_arg(ap, unsigned long);
_ultoa(ul, t, 16);
t += strlen(t);
break;
case 'p':
*t++ = '0'; *t++ = 'x';
v = va_arg(ap, voidp);
_ultoa((unsigned long)v, t, 16);
t += strlen(t);
break;
case 'c':
*t++ = va_arg(ap, int);
break;
case 'O':
l = (long)va_arg(ap, int);
_ltoa(l, buf, 10);
if (l <= 0 || l > OP_MAX) {
strcpy(t, "<<op ");
_ltoa(l, t+5, 10);
strcat(t, "?>>");
t += strlen(t);
} else {
_ltoa(l, t, 10);
strcat(t, " (");
strcat(t, op_names[l]);
strcat(t, ")");
t += strlen(t);
}
break;
case 'A':
ul = va_arg(ap, unsigned long);
_ultoa((unsigned long)(ul>>24), t, 10);
t += strlen(t); *t++ = '.';
_ultoa((unsigned long)((ul>>16)&0xFF), t, 10);
t += strlen(t); *t++ = '.';
_ultoa((unsigned long)((ul>>8)&0xFF), t, 10);
t += strlen(t); *t++ = '.';
_ultoa((unsigned long)(ul&0xFF), t, 10);
t += strlen(t);
break;
default:
strcpy(t, "<<<");
t[3] = *s;
strcpy(t+4, ">>>");
t += 7;
break;
}
}
*t++ = '\r'; *t++ = '\n'; *t = '\0';
va_end(ap);
Psem_obtain(DEBUG_SEM);
l = Fopen(DEBUG_LOG, 2);
if (l < 0) {
Cconws("Unable to open debug log ");
Cconws(DEBUG_LOG);
Cconws("\r\n");
} else {
fd = (int)(short)l;
Fseek(0L, fd, SEEK_END);
Fwrite(fd, strlen(line), line);
Fclose(fd);
}
Psem_release(DEBUG_SEM);
}
void cleanup_debug()
{
Psem_obtain(DEBUG_SEM);
Psem_destroy(DEBUG_SEM);
}
#endif /* DEBUG */